package com.eastfair.tag.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.tag.dao.TagResourceMapper;
import com.eastfair.tag.dto.TagResourceDTO;
import com.eastfair.tag.entity.TagResource;
import com.eastfair.tag.mydao.MyTagMapper;
import com.eastfair.tag.service.TagResourceService;
import com.eastfair.tag.vo.TagResourceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author zjn
 * @date 2022-03-11
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class TagResourceServiceImpl extends SuperServiceImpl<TagResourceMapper, TagResource> implements TagResourceService {

    @Autowired
    private MyTagMapper myTagMapper;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<TagResource> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    public R<List<TagResourceVO>> getTagResourceRelationList(List<TagResourceDTO> tagResourceDTOList) {
        if (CollectionUtil.isEmpty(tagResourceDTOList)) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        List<TagResourceVO> result = new ArrayList<>();
        tagResourceDTOList.forEach(tagResourceDTO -> {
            TagResourceVO tagResourceVO = new TagResourceVO();
            tagResourceVO.setResourceType(tagResourceDTO.getResourceType());
            tagResourceVO.setResourceId(tagResourceDTO.getResourceId());
            tagResourceVO.setTagVOList(myTagMapper.getByResource(tagResourceDTO.getResourceType(), tagResourceDTO.getResourceId()));
            result.add(tagResourceVO);
        });
        return R.success(result);
    }

    @Override
    public R<Boolean> saveTagResourceRelation(TagResourceDTO tagResourceDTO) {
        if (Objects.isNull(tagResourceDTO) || Objects.isNull(tagResourceDTO.getResourceId())
                || Objects.isNull(tagResourceDTO.getResourceType())
                || CollectionUtil.isEmpty(tagResourceDTO.getTagIdList())) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        // 删除原关联关系
        R<Boolean> deleteFlag = deleteTagResourceRelation(tagResourceDTO);
        if (!deleteFlag.getIsSuccess()) {
            return deleteFlag;
        }
        List<Long> tagIdList = tagResourceDTO.getTagIdList();

        // 批量生成雪花id
        List<Long> idList = DefaultUidGenerator.getBatUidOfProjectId(ContextUtil.getProjectId(), tagIdList.size());

        List<TagResource> bathParams = new ArrayList<>();
        for (int i = 0; i < tagIdList.size(); i++) {
            TagResource tagResource = TagResource.builder().id(idList.get(i)).projectId(ContextUtil.getProjectId())
                    .resourceType(tagResourceDTO.getResourceType())
                    .resourceId(tagResourceDTO.getResourceId())
                    .tagId(tagIdList.get(i)).build();
            bathParams.add(tagResource);
        }

        return R.success(saveBatch(bathParams));
    }

    @Override
    public R<Boolean> deleteTagResourceRelation(TagResourceDTO tagResourceDTO) {
        if (Objects.isNull(tagResourceDTO) || Objects.isNull(tagResourceDTO.getResourceId())
                || Objects.isNull(tagResourceDTO.getResourceType())) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        UpdateWrapper<TagResource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(TagResource::getResourceType, tagResourceDTO.getResourceType())
                .eq(TagResource::getResourceId, tagResourceDTO.getResourceId())
                .set(TagResource::getIsDeleted, BusinessConstant.DELETE_YES);
        return R.success(update(updateWrapper));
    }

}
